let { Router } = require("express");
let User = require("../model/User");
let JWT = require("jsonwebtoken");
let md5 = require("md5");
let path = require("path");
let multer = require("multer");

let router = new Router();

let str = "fswerwerq23etrdty342";

router.post("/register", async (req, res) => {
  let { username, password } = req.body;

  // 判断参数是否为空
  if (username == "" || password == "") {
    res.status(400).json({
      msg: "缺少参数",
    });
    return;
  }
  // 查询用户是否存在
  let users = await User.findOne({
    username,
  });
  if (users) {
    res.status(400).json({
      msg: "账号已存在",
    });
    return;
  }
  // 注册用户
  await User.create({
    username,
    password: md5(password),
  });
  res.status(200).json({
    msg: "注册成功",
  });
});

/* 
登录
url: /api/user/login
method: POST
params: username password
*/

router.post("/login", async (req, res) => {
  let { username, password } = req.body;
  // 判断参数是否为空
  if (username == "" || password == "") {
    res.status(400).json({
      msg: "缺少参数",
    });
    return;
  }
  // 查询用户是否存在，判断密码是否正确
  let users = await User.findOne(
    {
      username,
      password: md5(password),
    },
    {
      password: false,
      img: false,
    }
  ).lean();

  if (users) {
    // 生成token给客户端
    let token = JWT.sign(users, str, {
      expiresIn: "1d",
    });
    // 响应客户端token
    res.status(200).json({
      msg: "登录成功",
      token,
      data: users,
    });
    return;
  }
  res.status(400).json({
    msg: "账号或密码错误",
  });
});

router.use(require("../middleware/auth.js"));
/*
修改用户信息,更换头像 
 */
router.post("/images", async (req, res) => {
  const userid = req.userInfo._id;
  let { img } = req.body;
  await User.findByIdAndUpdate(userid, {
    img: img,
  });
  res.status(200).json({
    msg: "更换成功",
    img,
  });
});

// 修改密码
router.post("/password", async (req, res) => {
  const userid = req.userInfo._id;
  let { oldpassword, newpassword } = req.body;
  console.log(oldpassword, md5(oldpassword));
  let data = await User.findOneAndUpdate({ _id: userid, password: md5(oldpassword) }, {
    password: md5(newpassword),
  });
  console.log(data);
  if (data === null) {
    res.status(200).json({
      msg: "旧密码有误",
      state: false,
    });
    return
  }
  res.status(200).json({
    msg: "更换成功",
    state: true,
    data
  });
});

/* 
获取用户信息
*/
router.get("/getinfo", async (req, res) => {
  const userid = req.userInfo._id;
  let datas = await User.findOne(
    {
      _id: userid,
    },
    {
      password: false,
    }
  ).lean();

  res.status(200).json({
    datas,
  });
});

/* 
 注销用户
*/
router.post("/layout", async (req, res) => {
  const userid = req.userInfo._id;
  await User.deleteOne({
    _id: userid,
  });
  res.status(200).json({
    msg: "注销成功",
  });
});
/* 
检查用户是否登录了
url: /api/user/check_login
method: POST
header: authorization
*/
router.use(require("../middleware/auth.js"));
router.get("/check_login", async (req, res) => {
  console.log(req.userInfo);
  res.status(200).json({
    msg: "登录成功",
    data: req.userInfo,
  });
});

module.exports = router;
